package com.spic.business.datamanagement.accident.controller;

import com.spic.business.accident.analysisReport.domain.AccidentAnalysisReport;
import com.spic.business.accident.analysisReport.service.IAccidentAnalysisReportService;
import com.spic.business.accident.feedback.domain.AccidentExperenceBack;
import com.spic.business.accident.feedback.service.IAccidentExperenceBackService;
import com.spic.business.accident.instant.domain.AccidentInstantReport;
import com.spic.business.accident.instant.service.IAccidentInstantReportService;
import com.spic.business.accident.interevents.eviewreport.domain.InternalEventReviewReport;
import com.spic.business.accident.interevents.eviewreport.service.IInternalEventReviewReportService;
import com.spic.business.datamanagement.accident.entity.AccidentInfoDTO;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.system.api.RemoteDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author : 赵祥
 * create at:  2022/11/20  16:50
 * @Description :
 **/
@RestController
@RequestMapping("/accident/info")
public class AccidentInfoController extends BaseController {
    @Autowired
    private IAccidentInstantReportService accidentInstantReportService;
    @Autowired
    private IAccidentAnalysisReportService accidentAnalysisReportService;
    @Autowired
    private IAccidentExperenceBackService accidentExperenceBackService;
    @Autowired
    private IInternalEventReviewReportService iInternalEventReviewReportService;
    @Autowired
    private RemoteDeptService remoteDeptService;
    /**
     * 查询事故即时报告列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AccidentInstantReport accidentInstantReport)
    {
        //获取事故发生单位及子部门
        if (null != accidentInstantReport.getDeptId()){
            List<Long> deptIds = remoteDeptService.getDeptAndChildren(accidentInstantReport.getDeptId()).getData();
            accidentInstantReport.setDeptIds(deptIds);
        }
        startPage();
        List<AccidentInfoDTO> accidentInfoDTOList = new ArrayList<>();
        List<AccidentInstantReport> list = accidentInstantReportService.selectAccidentInstantReportList(accidentInstantReport);
        for (int i = 0; i < list.size(); i++) {
            AccidentInfoDTO accidentInfoDTO = new AccidentInfoDTO();
            AccidentInstantReport instantReport = list.get(i);
            String dirtyReason = instantReport.getDirtyReason();
            accidentInfoDTO.setAccidentInstantReport(instantReport);
            //事故分析报告
            AccidentAnalysisReport reportParam = new AccidentAnalysisReport();
            reportParam.setAccidentInstantId(String.valueOf(instantReport.getId()));
            List<AccidentAnalysisReport> accidentAnalysisReports = accidentAnalysisReportService.selectAccidentAnalysisReportList(reportParam);
            if(accidentAnalysisReports != null && accidentAnalysisReports.size() > 0) {
                AccidentAnalysisReport report = accidentAnalysisReports.get(0);
                accidentInfoDTO.setAccidentAnalysisReport(report);

                //复查报告
                InternalEventReviewReport internalEventReviewReport = new InternalEventReviewReport();
                internalEventReviewReport.setEventId(instantReport.getId());
                List<InternalEventReviewReport> internalEventReviewReports = iInternalEventReviewReportService.selectInternalEventReviewReportList(internalEventReviewReport);
                if(internalEventReviewReports != null && internalEventReviewReports.size() > 0) {
                    accidentInfoDTO.setInternalEventReviewReport(internalEventReviewReports.get(0));
                } else {
                    accidentInfoDTO.setInternalEventReviewReport(new InternalEventReviewReport());
                }
                //经验反馈单
                AccidentExperenceBack accidentExperenceBack = new AccidentExperenceBack();
                accidentExperenceBack.setAnalysisReportId(report.getId());
                List<AccidentExperenceBack> accidentExperenceBacks = accidentExperenceBackService.selectAccidentExperenceBackList(accidentExperenceBack);
                if(accidentExperenceBacks != null && accidentExperenceBacks.size() > 0) {
                    accidentInfoDTO.setAccidentExperenceBack(accidentExperenceBacks.get(0));
                } else {
                    accidentInfoDTO.setAccidentExperenceBack(new AccidentExperenceBack());
                    if(dirtyReason == null) {
                        instantReport.setDirtyReason("缺少事故经验反馈单");
                        accidentInstantReportService.updateAccidentInstantReportOnly(instantReport);
                    }
                }
            } else {
                if(dirtyReason == null) {
                    instantReport.setDirtyReason("缺少事故分析报告及事故经验反馈单");
                    accidentInstantReportService.updateAccidentInstantReportOnly(instantReport);
                }
                accidentInfoDTO.setAccidentAnalysisReport(new AccidentAnalysisReport());
                accidentInfoDTO.setAccidentExperenceBack(new AccidentExperenceBack());
                accidentInfoDTO.setInternalEventReviewReport(new InternalEventReviewReport());
            }
            accidentInfoDTOList.add(accidentInfoDTO);
        }
        return getDataTable(accidentInfoDTOList);
    }

    @GetMapping("/statistics")
    public AjaxResult statistics() {
        Map<String, Integer> map = accidentInstantReportService.statistics();
        return AjaxResult.success(map);
    }
}
